﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;

namespace DataAccess
{
    public class ProductDataAccess
    {
        public static void RegisterProduct(Product product)
        {
            SSDDEntities.Instance.DiscardChanges();
            SSDDEntities.Instance.Products.Add(product);
            SSDDEntities.Instance.SaveChanges();
        }
        public static void ModifyProduct(Product product)
        {
            SSDDEntities.Instance.DiscardChanges();
            var v = (from p in SSDDEntities.Instance.Products
                         where p.ProductId == product.ProductId
                         select p).First();
            v.Name = product.Name;
            v.Quality = product.Quality;
            v.Trademark = product.Trademark;

            SSDDEntities.Instance.SaveChanges();
        }
        public static List<Product> GetProductById(Product product)
        {
            SSDDEntities.Instance.DiscardChanges();
            return (from p in SSDDEntities.Instance.Products
                    where p.ProductId == product.ProductId
                    select p).ToList();
        }
        public static List<Product> GetAllProducts()
        {
            SSDDEntities.Instance.DiscardChanges();
            return ((from p in SSDDEntities.Instance.Products
                    select p).ToList());
        }

        public static List<ProductStore> GetProductInStore(string productId)
        {
            SSDDEntities.Instance.DiscardChanges();
            return ((from p in SSDDEntities.Instance.ProductStores
                     where p.ProductId==productId
                     select p).ToList());
        }

        public static List<ProductStore> GetProductInStock(string productId)
        {
            throw new NotImplementedException();
        }


        public static void SaverOrder(Order o)
        {
            SSDDEntities.Instance.DiscardChanges();
            SSDDEntities.Instance.Orders.Add(o);
            SSDDEntities.Instance.SaveChanges();
        }

        public static void SaveSale(Sale dataSale)
        {
            SSDDEntities.Instance.DiscardChanges();
            SSDDEntities.Instance.Sales.Add(dataSale);
            SSDDEntities.Instance.SaveChanges();
        }

        public static void CheckOutOrder(string branchId)
        {
            SSDDEntities.Instance.DiscardChanges();
            var q = (from p in SSDDEntities.Instance.Orders
                     where p.BranchRemoteId == branchId
                     select p).ToList();
            foreach (var v in q)
            {
                v.Delivered = true;
            }
            SSDDEntities.Instance.SaveChanges();
        }

        public static void DiscountProducts(Order o)
        {
            SSDDEntities.Instance.DiscardChanges();
            foreach (var orderDetail in o.OrderDetails)
            {

                var q = (from p in SSDDEntities.Instance.ProductStores
                         where p.ProductId==orderDetail.ProductId
                         select p).ToList();
                if(q.Count!=0)
                q.First().StoreQuantity -= orderDetail.Quantity;
                SSDDEntities.Instance.SaveChanges();
            }

        }
    }
}
